﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCenter.Core.Service;
using UCenter.EmailService.Models;
using UCenter.TemplateEngine;

namespace UCenter.EmailService
{
    /// <summary>
    /// The service of email
    /// </summary>
    public class MailService : ServiceBase
    {
        #region Public Methods

        /// <summary>
        /// Sends the specified mail.
        /// </summary>
        /// <param name="mail">The mail.</param>
        public void Send(MailContract mail)
        {
            try
            {
                OnBeforeEmailSent(mail, SentAction.Waiting);
                MailWaitItem waitItem = ConvertType(mail);
                LoadRepository<IMailRepository>().Save(waitItem);
                OnAfterEmailSent(mail, SentAction.Waiting);
            }
            catch
            {
                OnEmailSentFailed(mail, SentAction.Waiting);
            }
        }

        /// <summary>
        /// Sends the specified mails.
        /// </summary>
        /// <param name="mails">The mails.</param>
        public void Send(List<MailContract> mails)
        {
            try
            {
                OnBeforeEmailSent(mails, SentAction.Waiting);
                List<MailWaitItem> waitItems = new List<MailWaitItem>();
                LoadRepository<IMailRepository>().Save(waitItems);
                OnAfterEmailSent(mails, SentAction.Waiting);
            }
            catch
            {
                OnEmailSentFailed(mails, SentAction.Waiting);
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs before email sent
        /// </summary>
        public static event EventHandler<MailEventArgs> BeforeEmailSent;

        /// <summary>
        /// Occures after email sent
        /// </summary>
        public event EventHandler<MailEventArgs> AfterEmailSent;

        /// <summary>
        /// Occurs when email failed
        /// </summary>
        public event EventHandler<MailEventArgs> EmailSentFailed;

        #endregion

        #region Private Methods

        /// <summary>
        /// Converts the type.
        /// </summary>
        /// <param name="mail">The mail.</param>
        /// <returns></returns>
        private MailWaitItem ConvertType(MailContract mail)
        {
            MailWaitItem waitItem = new MailWaitItem();
            waitItem.Subject = mail.Subject;
            waitItem.Recipient = mail.Recipient;
            waitItem.Priority = (short)mail.Priority;
            waitItem.CreateDate = mail.CreateDate;
            waitItem.Content = TemplateService.Generate(mail.Content);
            waitItem.MailType = (short)mail.Category;
            waitItem.SentDate = mail.SentDate;
            waitItem.Retries = mail.Retries;

            return waitItem;
        }

        /// <summary>
        /// Converts the type.
        /// </summary>
        /// <param name="mails">The mails.</param>
        /// <returns></returns>
        private List<MailWaitItem> ConvertType(List<MailContract> mails)
        {
            List<MailWaitItem> waitItems = new List<MailWaitItem>();
            foreach (var item in mails)
	        {
                waitItems.Add(ConvertType(item));
	        }

            return waitItems;
        }

        #endregion

        #region Protected Virtual Methods

        /// <summary>
        /// Called when email sent.
        /// </summary>
        /// <param name="mail">The mail.</param>
        protected static void OnBeforeEmailSent(Object mail, SentAction action)
        {
            EventHandler<MailEventArgs> tempHandler = BeforeEmailSent;
            if (tempHandler != null)
            {
                tempHandler(mail, new MailEventArgs(mail, action));
            }
        }

        protected virtual void OnAfterEmailSent(Object mail, SentAction action)
        {
            EventHandler<MailEventArgs> tempHandler = AfterEmailSent;
            if (tempHandler != null)
            {
                tempHandler(mail, new MailEventArgs(mail, action));
            }
        }

        /// <summary>
        /// Called when email failed.
        /// </summary>
        /// <param name="mail">The mail.</param>
        protected virtual void OnEmailSentFailed(Object mail, SentAction action)
        {
            EventHandler<MailEventArgs> tempHandler = EmailSentFailed;
            if (tempHandler != null)
            {
                tempHandler(mail, new MailEventArgs(mail, action));
            }
        }

        #endregion
    }
}
